#include "Log.h"

#include "definitions.h"
#include "util.h"

Log Log::instance;

Log::Log() : logFile(LOG_FILE)
{
}

Log::~Log()
{

}

Log& Log::getInstance()
{
	return instance;
}

void Log::log(int userID, std::string msg, bool isError)
{
	std::string s;

	if (isError)
		s += "--- ";

	intToString(userID,s);

	s += ": ";

	logFile.tomarLock(lock_escritura);
	logFile.escribir(s.c_str(), s.size());
	logFile.escribir(msg.c_str(), msg.size());
	if (isError)
		logFile.escribir("--- ",4);
	logFile.escribir("\n",1);
	logFile.liberarLock();
}

void Log::logToScreen()
{
	dup2(2,instance.logFile.getFileDescriptor());
}

void Log::logScreenToFile()
{
	dup2(instance.logFile.getFileDescriptor(),2);
}
